#! /usr/bin/ruby
require 'database'
require 'server'
require 'agent'
USAGE = <<EOS

config-manager -- tool for remotely managing config files.
config-manager [-s] [-a] [-k]
  -s    Run as server
  -a    Run as agent
  -k    Kill off all config-manager processes
EOS

def command_line
  help = true if (ARGV[0] == '-h'.downcase)
  @run_as_agent = true if (ARGV[0] == '-a'.downcase)
  @run_as_server = true if (ARGV[0] == '-s'.downcase)
  help = true if (!@run_as_agent && !@run_as_server)
  puts USAGE if (help)
  exit if (help)

end

def start_server
  Database.test_connect
  server = Server.instance()
  server.start
  server.daemon
end

def start_agent
  Database.test_connect
  agent = Agent.instance()
  agent.start
  agent.daemon
end

command_line
start_server if (@run_as_server)
start_agent if (@run_as_agent)
